package tq.com.tqteacher.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.tqcom.net.INetCallback;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;
import tq.com.tqteacher.view.TQQNoManagerView;

public class TQLetShareModel extends TQNewPublish {

    private int shareCount;
    private ArrayList<TQAtachModel> mAttaches = new ArrayList<>();
    private int mAll;
    private int mUpdate;


    public void setShareCount(int pCount){
        shareCount = pCount;
    }

    public int getShareCount(){
        return shareCount;
    }
    public void onPulishComplete(INetCallback pNetCallback){
        HashMap<String, Object> param = new HashMap<>();
        param.put("category", Integer.valueOf(getType()));
        param.put("title", getName());
        param.put("desc", getDesc());
        param.put("grade", Integer.valueOf(getGrade()));
        param.put("chapter", Integer.valueOf(getParentId()));

        param.put("maxuser", Integer.valueOf(shareCount));

        TQNetInterface.newPublish(param, pNetCallback);
    }

    public void onModifyBaseInfo(INetCallback pNetCallback){
        HashMap<TQModify.MaterialBaseInfo, Object> param = new HashMap<>();
        param.put(TQModify.MaterialBaseInfo.title, getName());
        param.put(TQModify.MaterialBaseInfo.desc, getDesc());
        param.put(TQModify.MaterialBaseInfo.grade, String.valueOf(getGrade()));
        param.put(TQModify.MaterialBaseInfo.maxuser, String.valueOf(shareCount));

        TQNetInterface.modifyMaterialInfo(getSeq_id(), param, pNetCallback);
    }

    public void fromJson(JSONObject pObject) {
        super.fromJson(pObject);
        if (pObject == null) return;

        shareCount = pObject.optInt("max_user");
        mAll = pObject.optInt("max_attachemnt");
        mUpdate = pObject.optInt("cur_attachment");
    }

    public void refreshAttach(JSONArray pObject){
        if(pObject == null) return;

        mAttaches.clear();
        for(int i = 0; i < pObject.length(); i++){
            try {
                JSONObject lObject = pObject.getJSONObject(i);
                TQAtachModel lAtachModel = new TQAtachModel();
                lAtachModel.fromJson(lObject);

                mAttaches.add(lAtachModel);
            }catch (Exception pE){
                pE.printStackTrace();
            }
        }
    }

    public void addAttach(TQAtachModel pShareAttach){
        mAttaches.add(0, pShareAttach);
    }

    public ArrayList<TQAtachModel> getAttaches(){
        return mAttaches;
    }

    public int getMaxAttachement(){return mAll;}
    public int getCurrentAttachement(){return mUpdate;}
    public int getMaxUser(){return shareCount;}
}
